<?php

namespace App\Http\Controllers\Admin\Shop;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Admin\CommonController;
use App\Models\shop\Customer;

class CustomerController extends CommonController
{
    public function __construct()
    {
        parent::__construct();
        view()->share([
            '_system' => 'shop',
            '_customer' => 'am-active',
            '_shop' => 'am-in',
        ]);
    }

    //显示首页
    public function index(Request $request)
    {
//        return $request->all();exit;

        //多条件查找
        $where=function($query) use ($request){

            //会员昵称过滤
            if($request->has('nickname') && $request->nickname!='')
            {
                $nickname='%'.$request->nickname.'%';
                $query->where('nickname','like',$nickname);
            }


            //微信OPEN_ID过滤
            if($request->has('openid') && $request->openid!='')
            {
                $openid='%'.$request->openid.'%';
                $query->where('openid','like',$openid);
            }

            //性别过滤
            if($request->has('sex') && $request->sex!='-1')
            {
                $query->where('sex',$request->sex);
            }

            //时间过滤
            if($request->has('created_at') && $request->created_at!='')
            {
                $time=explode('~',$request->created_at); //$time是一个数组
                $time['0']=$time['0'].' 00:00:00';
                $time['1']=$time['1'].' 23:59:59';
                $query->whereBetween('created_at',$time);
            }
        };



        $customers=Customer::orderBy('created_at','desc')->
            where($where)->
        paginate(config('fangshop.page_size'));;
//        return $customers;exit;
        return view('admin.shop.customers.index')->with('customers',$customers);
    }

}
